package com.wwu.system.controller;

import com.wwu.common.constant.CommConst;
import com.wwu.common.enums.MenuBtnEnum;
import com.wwu.common.web.ResultObj;
import com.wwu.common.web.ResultPage;
import com.wwu.system.common.aspect.Log;
import com.wwu.system.entity.SysParam;
import com.wwu.system.service.ISysParamService;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Arrays;

/**
 * 参数配置前端控制器
 * @author 一蓑烟雨
 * @date 2023/5/31 9:37
 */
@Api(tags = "参数配置控制器(SysParamController)")
@ApiSort(3)
@Controller
@RequestMapping("/system/sysParam")
public class SysParamController {
    @Resource
    private ISysParamService sysParamService;

    /**
     * 请求转发到参数配置主页面
     * @return java.lang.String
     * @author 一蓑烟雨
     * @date 2023/7/11 14:54
     */
    @GetMapping("/paramMain")
    public String paramMainHtml(Model model){
        return "system/sysparam/param-main";
    }

    /**
     * 请求参数配置新增或更新页面
     * @param paramKey 参数键
     * @param model
     * @return java.lang.String
     * @author 一蓑烟雨
     * @date 2023/8/10 18:18
     */
    @GetMapping("/paramAddOrUpdate")
    public String paramAddOrUpdateHtml(String paramKey,Model model){
        if(StringUtils.hasText(paramKey)){
            model.addAttribute(CommConst.SYS_PARAM,sysParamService.getById(paramKey));
        }
        return "system/sysparam/param-add-edit";
    }

    @Log(menuName = "参数配置",menuBtnEnum = MenuBtnEnum.SHOW)
    @ApiOperation(value = "查询参数配置")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数",paramType = "query", required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数",paramType = "query", required = true),
            @ApiImplicitParam(name = "sysParam", value = "参数实体")
    })
    @GetMapping("/paramList")
    @ResponseBody
    public ResultPage<SysParam> paramList(@RequestParam(defaultValue = "1") Integer page,
                                          @RequestParam(defaultValue = "5000") Integer limit,
                                          SysParam sysParam){
        return sysParamService.getParamListPage(page,limit,sysParam);
    }


    @Log(menuName = "参数配置",menuBtnEnum = MenuBtnEnum.ADD)
    @ApiOperation(value = "添加参数配置")
    @ApiOperationSupport(order = 2)
    @PostMapping("/saveParam")
    @ResponseBody
    public  ResultObj saveParam(@RequestBody SysParam sysParam){
        sysParamService.saveParam(sysParam);
        return ResultObj.success("参数配置添加成功");
    }

    @Log(menuName = "参数配置",menuBtnEnum = MenuBtnEnum.EDIT)
    @ApiOperation(value = "更新参数配置")
    @ApiOperationSupport(order = 3)
    @PutMapping("/updateParam")
    @ResponseBody
    public ResultObj updateParam(@RequestBody SysParam sysParam){
        sysParamService.updateParam(sysParam);
        return ResultObj.success("参数配置更新成功");
    }

    @Log(menuName = "参数配置",menuBtnEnum = MenuBtnEnum.STATUS)
    @ApiOperation(value = "更新参数配置状态")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "paramKey", value = "参数key", required = true),
            @ApiImplicitParam(name = "paramStatus", value = "参数状态(0:启用;1:作废)",required = true)
    })
    @PutMapping("/updateParamStatus")
    @ResponseBody
    public ResultObj updateParamStatus(String paramKey,Integer paramStatus){
        sysParamService.updateParamStatus(paramKey,paramStatus);
        return ResultObj.success("参数配置状态更新成功");
    }

    @Log(menuName = "参数配置",menuBtnEnum = MenuBtnEnum.DEL)
    @ApiOperation(value = "删除参数配置")
    @ApiOperationSupport(order = 5)
    @DeleteMapping("/deleteParam/{paramKeys}")
    @ResponseBody
    public ResultObj deleteParam(@ApiParam(name = "paramKeys", value = "参数key集合，以逗号分隔", required = true)
                                @PathVariable String paramKeys){
        sysParamService.deleteParam(Arrays.asList(paramKeys.split(CommConst.SPLIT_COMMA)));
        return ResultObj.success("参数配置删除成功");
    }
}